//    The page gives admin the functionality to Add / Edit Medical School        
//    The data will be inserted/updated to the tbl_medicalschool table.                                
//    Author: Rupinder Saini                                                                    
//    Date: May 01,2006  

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;


public partial class Admin_Ad_MedicalSchool : System.Web.UI.Page
{
    public int id = 0;
    // creating an instance of manager class
    Manager mgr = new Manager(true,"ConnectionString");
          
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
        }
        catch { id = 0; }
        //To show if adding or editing Medical School.
        lbl_heading.Text = id == 0 ? "Add Medical School" : "Edit Medical School";
        if (!IsPostBack)
        {// check page validity
            Page.Validate();

            if (id > 0)
            {// pre-fill page contents if edit mode
                loadcontents();
            }
        }


    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        check_validations();
        if (Page.IsValid)
        {
            string sql = "";
            int txtrating = 0;
            // if the rating is greater than 100 show it as 100
            if (Convert.ToInt32(Convert.ToDouble(txt_rate.Text)) > 100)
            {
                txtrating = 100;
            }
            else
            {
                txtrating = Convert.ToInt32(Convert.ToDouble(txt_rate.Text));
            }
            // get adoc rating value
            decimal Adocrating = (Convert.ToDecimal(5.00 - 3.33) * Convert.ToDecimal(txtrating) / Convert.ToDecimal(100) + Convert.ToDecimal(3.33));
          
            if (id == 0) // add mode
            {
                SqlParameter[] sqlParam = new SqlParameter[2];
                sqlParam[0] = new SqlParameter("@MedicalSchool", SqlDbType.VarChar, 500);
                sqlParam[1] = new SqlParameter("@rating", SqlDbType.Decimal);
                sqlParam[1].Precision = 18;
                sqlParam[1].Scale = 6;
                sqlParam[0].Value = txt_name.Text;
                sqlParam[1].Value = Adocrating;
                //checking for duplicacy while adding new
                sql = "select count(*) from tbl_MedicalSchool where MedicalSchool='" + txt_name.Text.Replace("'", "''").Trim() + "' and  isDelete=0 ";
                bool duplicate=check_dulicate(sql);
             //   if (duplicate==true)
                {
                 try
                {
                    mgr.ExecuteUpdate("Upload_Medicalschool", sqlParam);
                    Response.Redirect("ad_ManageMedSchool.aspx");                    
                }
                catch (Exception es)
                {
                    Response.Write(es.ToString());
                    lbl_error.Visible = true;
                    lbl_error.ForeColor = System.Drawing.Color.Red;
                    lbl_error.Text = es.ToString();
                }
                }
            }
            // editing
            else
            { // check dupicacy  while editing
                sql = "select count(*) from tbl_MedicalSchool where MedicalSchool='" + txt_name.Text.Replace("'", "''").Trim() + "' and id<>" + id + " and  isDelete=0 ";
                bool duplicate = check_dulicate(sql);
                if (duplicate==true)
                {
                     SqlParameter[] sqlParamup = new SqlParameter[3];
                    sqlParamup[0] = new SqlParameter("@mname", SqlDbType.VarChar, 500);
                    sqlParamup[1] = new SqlParameter("@rating", SqlDbType.Decimal);
                    sqlParamup[2] = new SqlParameter("@id", SqlDbType.Int);
                    sqlParamup[1].Precision = 18;
                    sqlParamup[1].Scale = 6;
                    sqlParamup[0].Value = txt_name.Text;
                    sqlParamup[1].Value = Adocrating;
                    sqlParamup[2].Value = id;
                    try
                    {
                        mgr.ExecuteUpdate("update_Medicalschool", sqlParamup);
//                        JSFileCreator jc = new JSFileCreator();
  //                      jc.CreateMedSchoolJS();
                        Response.Redirect("ad_ManageMedSchool.aspx");
                    }
                    catch (Exception es)
                    {
                        Response.Write(es.ToString());
                        lbl_error.Visible = true;
                        lbl_error.ForeColor = System.Drawing.Color.Red;
                        lbl_error.Text = es.ToString();
                    }
                   
                }
            }
        }

    }

    protected void btn_cancel_Click(object sender, EventArgs e)
    {
      
        //If Cancel button is clicked than the user will be redirected to the main page.
        if (id > 0)
            Response.Redirect("ad_ManageMedSchool.aspx");
        else
            Response.Redirect("ADadminAccess.aspx");
    }

    #region generalfunctions

    /// <summary>
    /// check the validations
    /// </summary>
    public void check_validations()
    {
        rval_mname.Enabled = true;
        rval_mname.Validate();
        rval_rating.Enabled = true;
        rval_rating.Validate();
    }

    /// <summary>
    /// pre-fill controls if edit mode
    /// </summary>

    public void loadcontents()
    {
        if (id > 0)
        {
            DataSet ds = mgr.ExecuteQuery("select * from tbl_MedicalSchool where  isdelete=0 and id=" + id, "tbl");
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_name.Text = ds.Tables[0].Rows[0]["MedicalSchool"].ToString();
                // converting to US rating that was entered by user
                decimal usrate = Convert.ToDecimal((Convert.ToDouble(ds.Tables[0].Rows[0]["Rating"].ToString()) - 3.33) * 100 / (5 - 3.33));
                
                txt_rate.Text = usrate.ToString();
                // p=((5 - 3.33) * Convert.ToDouble(txt_rate.Text) + 3.33);
                

            }
        }
    
    }

    /// <summary>
    /// check for dupliacte name
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public bool check_dulicate(string sql)
    {
        DataSet dsname = mgr.ExecuteQuery(sql, "tbl1");
        if (dsname.Tables[0].Rows.Count > 0)
        {
            if (Convert.ToInt32(dsname.Tables[0].Rows[0][0].ToString()) > 0)
            {
                {
                    lbl_error.Visible = true;
                    lbl_error.ForeColor = System.Drawing.Color.Red;
                    lbl_error.Text = "This Medical School name already exists. Please choose another name !!";
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else
        {
            return true;
        }
    }

    #endregion
}
